package com.quanshu.exchange.ui.gui.message.chat.input;

import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jakewharton.rxbinding2.widget.TextViewAfterTextChangeEvent;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.utils.RxViewHelper;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.subjects.BehaviorSubject;

/**
 * Created by jye on 2017/12/11.
 */

public class ChatInputView extends RelativeLayout {

    @BindView(R.id.btn_set_mode_voice)
    Button btnSetVoice;
    @BindView(R.id.iv_face_normal)
    ImageView ivFaceNormal;
    @BindView(R.id.rl_face)
    RelativeLayout rlFace;
    @BindView(R.id.et_sendmessage)
    EditText etSendmessage;
    @BindView(R.id.edittext_layout)
    RelativeLayout edittextLayout;
    @BindView(R.id.btn_more)
    Button btnMore;

    public TextView getBtnSend() {
        return btnSend;
    }

    @BindView(R.id.btn_send)
    TextView btnSend;
    @BindView(R.id.chat_main_menu_layout)
    LinearLayout chatMainMenuLayout;
    private View view;

    protected InputMethodManager inputManager;

    public BehaviorSubject getExpressionClick() {
        return expressionClick;
    }

    public BehaviorSubject getExtendClick() {
        return extendClick;
    }

    public BehaviorSubject<String> getSendClick() {
        return sendClick;
    }

    public BehaviorSubject getVoiceClick() {
        return voiceClick;
    }

    private BehaviorSubject voiceClick = BehaviorSubject.create();
    private BehaviorSubject expressionClick = BehaviorSubject.create();
    private BehaviorSubject extendClick = BehaviorSubject.create();
    private BehaviorSubject<String> sendClick = BehaviorSubject.create();

    public ChatInputView(Context context) {
        super(context);
        initView(context);
    }

    public ChatInputView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public ChatInputView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public ChatInputView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView(context);
    }

    private void initView(Context context) {
        view = LayoutInflater.from(context).inflate(R.layout.chat_input_view, this);
        ButterKnife.bind(view);
        initListener();
        inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
    }

    private void initListener() {
        RxViewHelper.getInstance().clicks(btnSetVoice, aVoid -> voiceClick.onNext(aVoid));
        RxViewHelper.getInstance().clicks(rlFace, aVoid -> expressionClick.onNext(aVoid));
        RxViewHelper.getInstance().clicks(btnMore, aVoid -> extendClick.onNext(aVoid));
        RxViewHelper.getInstance().clicks(btnSend, aVoid -> {
            sendClick.onNext(etSendmessage.getText().toString());
            etSendmessage.setText("");
        });
        RxViewHelper.getInstance().afterTextChangeEvents(etSendmessage,
                textViewAfterTextChangeEvent -> setSendBtnVisibility(textViewAfterTextChangeEvent));

    }

    private void setSendBtnVisibility(TextViewAfterTextChangeEvent textViewAfterTextChangeEvent) {
        String str = textViewAfterTextChangeEvent.editable().toString();
        if (TextUtils.isEmpty(str)) {
            btnSend.setVisibility(View.GONE);
        } else {
            btnSend.setVisibility(View.VISIBLE);
        }
    }

    public void setInputText(CharSequence charSequence) {
        etSendmessage.append(charSequence);
    }

    public EditText getInputText() {
        return etSendmessage;
    }

    /**
     * hide keyboard
     */
    public void hideKeyboard() {
        if (inputManager != null) {
            if (inputManager.isActive()) {
                requestFocus();
                inputManager.hideSoftInputFromWindow(getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    }

}
